package com.bookm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bookm.bean.Book;
import com.bookm.service.MPBookService;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;

@RestController
@RequestMapping("/books")
public class BookController {
    @Autowired
    private MPBookService MPBookService;

    @GetMapping
    public List<Book>getAll(){
        return MPBookService.list();
    }
    @GetMapping("{id}")
    public Book getById(@PathVariable Integer id){//上下两个id对应
        return MPBookService.getById(id);
    }

    @PostMapping
    public Boolean save(@RequestBody  Book book){
        return MPBookService.save(book);
    }
    @PutMapping
    public Boolean update(@RequestBody Book book){
        return MPBookService.updateById(book);
    }

    @DeleteMapping
    public Boolean delete(Integer id){
        return MPBookService.removeById(id);
    }


    @GetMapping("{currentPage}/{pageSize}")
    public IPage<Book>getPage(@PathVariable int currentPage,@PathVariable int pageSize){
      return   MPBookService.getPage(currentPage,pageSize);
    }
    @RequestMapping("0")
    String test(){
        return "成功";
    }


    @RequestMapping("1")
    public  void test1() {
        try {
            long t1 = System.currentTimeMillis();
            SXSSFWorkbook workbook = new SXSSFWorkbook();
            workbook.createSheet("aaa");
            SXSSFSheet aaa = workbook.getSheetAt(0);
            for (int i = 0; i < 1000000; i++) {
                aaa.createRow(i);
                aaa.getRow(i).createCell(0).setCellValue("aaaaaaaaaaaaaaaaaaaaaaa");
                aaa.getRow(i).createCell(1).setCellValue("aaaaaaaaaaaaaaaaaaaaaaa");
                aaa.getRow(i).createCell(2).setCellValue("aaaaaaaaaaaaaaaaaaaaaaa");
                aaa.getRow(i).createCell(3).setCellValue("aaaaaaaaaaaaaaaaaaaaaaa");
                aaa.getRow(i).createCell(4).setCellValue("aaaaaaaaaaaaaaaaaaaaaaa");
            }
            OutputStream outputStream = null;
            // 打开目的输入流，不存在则会创建
            outputStream = new FileOutputStream("out.xlsx");
            workbook.write(outputStream);
            outputStream.close();
            long t2 = System.currentTimeMillis();
            System.out.println("SXSSFWorkbook : 100w条数据写入Excel 消耗时间：" + (t2 - t1));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * SXSSFWorkbook : 1w条数据写入Excel 消耗时间：2151
     * SXSSFWorkbook : 100w条数据写入Excel 消耗时间：215100,  如果说组织分配
     */
    @RequestMapping("2")
    public  void test2() {

        try {
            long t1 = System.currentTimeMillis();
            XSSFWorkbook workbook = new XSSFWorkbook();
            workbook.createSheet("aaa");
            XSSFSheet aaa = workbook.getSheetAt(0);
            for (int i = 0; i < 1000000; i++) {
                aaa.createRow(i);
                aaa.getRow(i).createCell(0).setCellValue("aaaaaaaaaaaaaaaaaaaaaaa");
                aaa.getRow(i).createCell(1).setCellValue("aaaaaaaaaaaaaaaaaaaaaaa");
                aaa.getRow(i).createCell(2).setCellValue("aaaaaaaaaaaaaaaaaaaaaaa");
                aaa.getRow(i).createCell(3).setCellValue("aaaaaaaaaaaaaaaaaaaaaaa");
                aaa.getRow(i).createCell(4).setCellValue("aaaaaaaaaaaaaaaaaaaaaaa");
            }
            OutputStream outputStream = null;
            // 打开目的输入流，不存在则会创建
            outputStream = new FileOutputStream("out2.xlsx");
            workbook.write(outputStream);
            outputStream.close();
            long t2 = System.currentTimeMillis();
            System.out.println("XSSFWorkbook : 100w条数据写入Excel 消耗时间：" + (t2 - t1));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
